package net.zjitc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.zjitc.dao.UserMapper;
import net.zjitc.domain.User;
import net.zjitc.service.UserService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public Boolean addUser(User user) {
        return userMapper.insert(user) == 1;
    }

    @Override
    public Boolean deleteUser(Integer id) {
        return userMapper.deleteById(id) == 1;
    }

    @Override
    public User findById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public Boolean updateUser(User user) {
        return userMapper.updateById(user) == 1;
    }

    @Override
    public List<User> findPage(int currentPage, int pageSize, User user) {
        IPage page = new Page(currentPage, pageSize);
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Strings.isNotEmpty(user.getUsername()), User::getUsername, user.getUsername());
        wrapper.like(Strings.isNotEmpty(user.getGender()), User::getGender, user.getGender());
        wrapper.like(Strings.isNotEmpty(user.getEmail()), User::getEmail, user.getEmail());
        userMapper.selectPage(page, wrapper);
        return page.getRecords();
    }
}
